package com.example.mybaiduweishi.activity;

import android.app.FragmentManager;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.mybaiduweishi.R;
import com.example.mybaiduweishi.activity.base.BaseActivity;
import com.example.mybaiduweishi.fragments.UpdateVersionFragment;
import com.example.mybaiduweishi.vo.VersionInfo;

import butterknife.BindView;
import butterknife.OnClick;

public class UpdateActivity extends BaseActivity {

    @BindView(R.id.toolbar_update)
    Toolbar toolbar;
    @BindView(R.id.layout_banben)
    LinearLayout layoutBanben;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                // overridePendingTransition(R.anim.);
            }
        });
    }

    public int getContentXML() {
        return R.layout.activity_update;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {

    }

    public void showDialog(String msg){
        FragmentManager fm1=getFragmentManager();
        UpdateVersionFragment u1= UpdateVersionFragment.getUpdateVersionFragment(msg);
        u1.show(fm1,msg);
    }

    @OnClick(R.id.layout_banben)
    public void onViewClicked() {
        //有一个返回值
        startActivityForResult(new Intent(UpdateActivity.this,Update_banbenActivity.class),0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case 2:
                showDialog("- -，网络断开，无法连接上服务器");
                break;
            case 1:
                showDialog("网络太慢了，无法下载");
                break;
            case 3:
                //接受第二个参数传过来的值
                VersionInfo versionInfo = (VersionInfo) data.getSerializableExtra("VersionInfo");
                FragmentManager fm = getFragmentManager();
                UpdateVersionFragment uvf = UpdateVersionFragment.getUpdateVersionFragment(versionInfo);  //装载序列化的实体
                uvf.show(fm,"");
                break;
            case 4:
                showDialog("恭喜，已经是最新版本了！！！");
                break;
            default:
                Toast.makeText(this, "resultCode:"+resultCode, Toast.LENGTH_SHORT).show();
                break;

        }
    }
}